package com.ruoyi.project.exam.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.QuType;
import com.ruoyi.common.utils.IdGen;
import com.ruoyi.common.utils.security.ShiroUtils;

import com.ruoyi.project.exam.domain.PaperModel;
import com.ruoyi.project.exam.domain.PaperQuAnswerModel;
import com.ruoyi.project.exam.domain.PaperQuModel;
import com.ruoyi.project.exam.domain.QuModel;
import com.ruoyi.project.exam.domain.UserExamModel;
import com.ruoyi.project.exam.mapper.PaperMapper;
import com.ruoyi.project.exam.mapper.PaperQuAnswerMapper;
import com.ruoyi.project.exam.mapper.PaperQuMapper;
import com.ruoyi.project.exam.mapper.UserExamMapper;
import com.ruoyi.project.exam.service.IPaperService;


@Service
public class PaperServiceImpl implements IPaperService{
	@Autowired
	private PaperMapper paperMapper;
	@Autowired
	private PaperQuMapper  paperQuMapper;
	@Autowired
	private UserExamMapper userExamMapper;
	
	
	@Override
	public PaperModel selectPaperDetail(String examPaperId) {
		PaperModel paperModel = paperMapper.selectPaperByExamPaperId(examPaperId);
		if(paperModel != null){
			List<PaperQuModel> list = paperQuMapper.selectPaperQuByExamPaperId(examPaperId);
			List<PaperQuModel> radioList = new ArrayList<PaperQuModel>();
			List<PaperQuModel> multiList = new ArrayList<PaperQuModel>();
			List<PaperQuModel> judgeList = new ArrayList<PaperQuModel>();
			if(list != null && !list.isEmpty()){
				for(int i = 0; i < list.size(); i++){
					if(QuType.RADIO.getType().equals(list.get(i).getQuType())){
						radioList.add(list.get(i));
					}
					if(QuType.MULTI.getType().equals(list.get(i).getQuType())){
						multiList.add(list.get(i));
					}
					if(QuType.JUDGE.getType().equals(list.get(i).getQuType())){
						judgeList.add(list.get(i));
					}
				}
				Collections.sort(radioList, new Comparator<PaperQuModel>(){
					@Override
					public int compare(PaperQuModel o1, PaperQuModel o2) {
						return o1.getSort()-o2.getSort();
					}
				});
				Collections.sort(multiList, new Comparator<PaperQuModel>(){
					@Override
					public int compare(PaperQuModel o1, PaperQuModel o2) {
						return o1.getSort()-o2.getSort();
					}
				});
				Collections.sort(judgeList, new Comparator<PaperQuModel>(){
					@Override
					public int compare(PaperQuModel o1, PaperQuModel o2) {
						return o1.getSort()-o2.getSort();
					}
				});
				paperModel.setRadioList(radioList);
				paperModel.setMultiList(multiList);
				paperModel.setJudgeList(judgeList);
			}
		}
		return paperModel;
	}


	@Override
	@Transactional
	public Map<String, Object> addPaper(PaperModel model) {
		Map<String, Object> map =  new HashMap<String, Object>();
		model.setUserId(ShiroUtils.getUserId());
		model.setCreateTime(new Date());
		Integer userScore = 0;
		List<PaperQuAnswerModel> quAnswerList = new ArrayList<PaperQuAnswerModel>();
		if(model.getPaperQuList() != null && !model.getPaperQuList().isEmpty()){
			for(int i = 0 ; i < model.getPaperQuList().size(); i++){
				PaperQuModel quModel = model.getPaperQuList().get(i);
				quModel.setId(IdGen.uuid());
				quModel.setPaperId(model.getId());
				//是否正确
				boolean right = true;
				for(PaperQuAnswerModel answerModel : quModel.getAnswerList()){
					answerModel.setId(IdGen.uuid());
					answerModel.setPaperId(model.getId());
					if(answerModel.getIsRight() != null && !answerModel.getIsRight() == answerModel.getChecked()){
						right = false;
					}
					quAnswerList.add(answerModel);
				}
				if(right){
					userScore += quModel.getScore();
					quModel.setActualScore(quModel.getScore());
				}
				quModel.setIsRight(right);
			}
			model.setUserScore(userScore);
		}
		model.setUserTime((long)(model.getTotalTime()*60)-model.getLeftSeconds());
		UserExamModel userExamModel = userExamMapper.selectUserExamByUserExamId(model.getUserId(), model.getExamId());
		if(userExamModel == null){
			userExamModel = new UserExamModel();
			userExamModel.setId(IdGen.uuid());
			userExamModel.setUserId(model.getUserId());
			userExamModel.setExamId(model.getExamId());
			userExamModel.setTryCount(Constants.TRY_COUNT);
			userExamModel.setMaxScore(model.getUserScore());
			userExamModel.setPassed(model.getUserScore() >= model.getQualifyScore() ? true : false);
			userExamModel.setCreateTime(new Date());
			userExamMapper.insertUserExam(userExamModel);
		}else{
			userExamModel.setTryCount((userExamModel.getTryCount()+1));
			userExamModel.setUpdateTime(new Date());
			if(model.getUserScore() > userExamModel.getMaxScore()){
				userExamModel.setMaxScore(model.getUserScore());
				userExamModel.setPassed(model.getUserScore() >= model.getQualifyScore() ? true : false);
			}
			userExamMapper.updateUserExam(userExamModel);
		}
		paperMapper.insertPaper(model, quAnswerList);
		map.put("userScore", model.getUserScore());
		map.put("passed", model.getUserScore() >= model.getQualifyScore() ? true : false);
		return map;
	}


	@Override
	public PaperModel selectUserExamPaper(String id) {
		PaperModel model = paperMapper.selectUserExamPaper(id);
		if(model != null){
			List<PaperQuModel> list = paperQuMapper.selectPaperQuByPaperId(id);
			model.setPaperQuList(list);
		}
		return model;
	}


	@Override
	public List<PaperModel> selectUserPaperList(PaperModel model) {
		return paperMapper.selectUserPaperList(model);
	}

}
